package com.bhs.sansong.ui.message;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.ArrayMap;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.PictureShowAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.bean.CircleFriendsBean;
import com.bhs.sansong.bean.CircleFriendsData;
import com.bhs.sansong.bean.CirclePersonageBean;
import com.bhs.sansong.bean.PictureShowBean;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.ui.findLogistics.CompanyDetailsActivity;
import com.bhs.sansong.utils.LogUtils;
import com.bhs.sansong.utils.MyUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 圈子个人页
 */
public class CirclePersonageActivity extends BaseActivity implements NetCallBack, View.OnClickListener {

    private TextView mTvNickName;
    private TextView mTvCompanyName;
    private TextView mTvAddress;
    private RecyclerView mRvList;
    private LinearLayout mLlCompanyDetail;
    private LinearLayout mLlSendMessage;
    private LinearLayout mLlVoiceCommunication;
    private String uid;
    private PictureShowAdapter mAdapter;
    private List<PictureShowBean> list = new ArrayList<>();
    private ImageView mIvFace;
    private int logistics_id;
    private String face;
    private String BgCover;
    @Override
    public int initLayout() {
        return R.layout.activity_circle_personage;
    }

    @Override
    public void initView() {
        logistics_id = getIntent().getIntExtra("logistics_id", 0);
        uid = getIntent().getStringExtra("uid");
        mTvNickName = findViewById(R.id.tv_nick_name);
        mTvCompanyName = findViewById(R.id.tv_company_name);
        mTvAddress = findViewById(R.id.tv_address);
        mRvList = findViewById(R.id.rv_list);
        mLlCompanyDetail = findViewById(R.id.ll_company_detail);
        mLlSendMessage = findViewById(R.id.ll_send_message);
        mLlVoiceCommunication = findViewById(R.id.ll_voice_communication);
        findViewById(R.id.ll_circle).setOnClickListener(this);
        mIvFace = findViewById(R.id.iv_face);
        if (logistics_id > 0) {
            mLlCompanyDetail.setVisibility(View.VISIBLE);
            mLlCompanyDetail.setOnClickListener(this);
        }
    }

    @Override
    public void initData() {
        mAdapter = new PictureShowAdapter(R.layout.item_picture5);
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(mActivity, RecyclerView.HORIZONTAL, false));
        loadingPopupShow();
        networkRequest(netApi.get_user_infother(uid), this, 777);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                gotoPage();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        Map<String, String> map = new ArrayMap<>();
        map.put("page", "1");
        map.put("limit", "4");
        map.put("keyword_unid", uid);
        networkRequest(netApi.circleList(map), this, 888);
    }

    private void gotoPage() {
        Bundle bundle = new Bundle();
        bundle.putString("name", mTvNickName.getText().toString());
        bundle.putString("face", face);
        bundle.putString("uid", uid);
        bundle.putString("BgCover", BgCover);
        startActivity(new Intent(mActivity, CircleFriendsActivity.class)
                .putExtras(bundle));
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 888) {
            loadingPopupDismiss();
            CircleFriendsData dataBean = new Gson().fromJson(json, CircleFriendsData.class);
            if (dataBean.getCode().equals("1")) {
                list.clear();
                for (CircleFriendsBean circleFriendsBean : dataBean.getData().getList()) {
                    if (!MyUtils.isEmpty(circleFriendsBean.getResource())) {
                        list.add(new PictureShowBean((String) circleFriendsBean.getResource().get(0)));
                    }
                }
                mAdapter.setNewData(list);
            }
        } else if (requestCode == 777) {

            CirclePersonageBean bean = new Gson().fromJson(json, CirclePersonageBean.class);
            if (bean.getCode().equals("1")) {
                if (logistics_id > 0) {
                    face = bean.getData().getCompany_pic();
                    mTvNickName.setText(bean.getData().getCompany_name());
                    mTvAddress.setText(String.format("公司地址：%s", bean.getData().getAddress()));
                } else {
                    face = bean.getData().getHeaderpic();
                    mTvNickName.setText(bean.getData().getNickname());
                    mTvAddress.setVisibility(View.GONE);
                }
                BgCover=bean.getData().getCircle_pic();
                Glide.with(mActivity).load(face).into(mIvFace);
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {
        loadingPopupDismiss();
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_circle:
                gotoPage();
                break;
            case R.id.ll_company_detail:
                startActivity(new Intent(mActivity, CompanyDetailsActivity.class)
                        .putExtra("logistics_id", logistics_id));
                break;
        }
    }
}